package com.aidex.biz.service.impl;

import com.aidex.biz.domain.CapBill;
import com.aidex.biz.domain.vo.OrgTypeStatisticVo;
import com.aidex.biz.mapper.CapBillMapper;
import com.aidex.biz.service.CapBillService;
import com.aidex.common.annotation.DataScope;
import com.aidex.common.core.page.PageDomain;
import com.aidex.common.core.service.BaseServiceImpl;
import com.aidex.common.utils.NumberUtils;
import com.aidex.common.utils.StringUtils;
import com.aidex.common.utils.sql.SqlUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 资方结算Service业务层处理
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2023-12-17
 */
@Service
@Transactional(readOnly = true)
public class CapBillServiceImpl extends BaseServiceImpl<CapBillMapper, CapBill> implements CapBillService {
    private static final Logger log = LoggerFactory.getLogger(CapBillServiceImpl.class);

    /**
     * 获取单条数据
     *
     * @param capBill 资方结算
     * @return 资方结算
     */
    @Override
    public CapBill get(CapBill capBill) {
        CapBill dto = super.get(capBill);
        return dto;
    }

    /**
     * 获取单条数据
     *
     * @param id 资方结算id
     * @return 资方结算
     */
    @Override
    public CapBill get(String id) {
        CapBill dto = super.get(id);
        return dto;
    }


    /**
     * 查询资方结算列表
     *
     * @param capBill 资方结算
     * @return 资方结算
     */
    @Override
    @DataScope(userAlias = "a", deptAlias = "a")
    public List<CapBill> findList(CapBill capBill) {
        return super.findList(capBill);
    }

    /**
     * 分页查询资方结算列表
     *
     * @param capBill 资方结算
     * @return 资方结算
     */
    @Override
    @DataScope(userAlias = "a", deptAlias = "a")
    public PageInfo<CapBill> findPage(CapBill capBill) {
        if (StringUtils.isNotNull(capBill.getIds()) && StringUtils.isNotEmpty(capBill.getIds())) {
            String[] idsArray = capBill.getIds().split(",");
            capBill.setIdsArray(idsArray);
        }
        return super.findPage(capBill);
    }

    /**
     * 保存资方结算
     *
     * @param capBill
     * @return 结果
     */
    @Override
    public boolean save(CapBill capBill) {
        return super.save(capBill);
    }


    /**
     * 删除资方结算信息
     *
     * @param capBill
     * @return 结果
     */
    @Override
    public boolean remove(CapBill capBill) {
        return super.remove(capBill);
    }

    /**
     * 批量删除资方结算
     *
     * @param ids 需要删除的资方结算ID
     * @return 结果
     */
    @Transactional(readOnly = false)
    @Override
    public int deleteCapBillByIds(String[] ids) {
        return mapper.deleteCapBillByIds(ids);
    }

    /**
     * 获取最大编号
     *
     * @param capBill 资方结算
     * @return 结果
     */
    @Override
    public int findMaxSort(CapBill capBill) {
        return NumberUtils.nextOrder(mapper.findMaxSort(capBill));
    }

    public CapBill findOne(CapBill capBill) {
        return mapper.findOne(capBill);
    }

    @Override
    @DataScope(userAlias = "a", deptAlias = "a")
    public PageInfo<OrgTypeStatisticVo> orgTypeStatistic(CapBill capBill) {
        PageDomain page = capBill.getPage();
        Integer pageNum = page.getPageNum();
        Integer pageSize = page.getPageSize();
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize)) {
            String orderBy = SqlUtil.escapeOrderBySql(page.getOrderBy());
            Boolean reasonable = page.getReasonable();
            PageHelper.startPage(pageNum, pageSize, orderBy).setReasonable(reasonable);
        }
        return new PageInfo(mapper.orgTypeStatistic(capBill));
    }

    @Override
    @DataScope(userAlias = "a", deptAlias = "a")
    public OrgTypeStatisticVo sumOrgTypeStatistic(CapBill capBill) {
        return mapper.sumOrgTypeStatistic(capBill);
    }

    @Override
    @DataScope(userAlias = "a", deptAlias = "a")
    public List<OrgTypeStatisticVo> ExportOrgTypeStatistic(CapBill capBill) {
        return mapper.orgTypeStatistic(capBill);
    }

    @Override
    @DataScope(userAlias = "a", deptAlias = "a")
    public List<OrgTypeStatisticVo> orgTypeDetailStatistic(CapBill capBill) {
        return mapper.orgTypeDetailStatistic(capBill);
    }

    @Transactional(readOnly = false)
    @Override
    public int deleteById(String id) {
        return mapper.deleteById(id);
    }

    @Override
    public PageInfo<OrgTypeStatisticVo> orgTypeDetailStatisticRecords(CapBill capBill) {
        PageDomain page = capBill.getPage();
        Integer pageNum = page.getPageNum();
        Integer pageSize = page.getPageSize();
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize)) {
            String orderBy = SqlUtil.escapeOrderBySql(page.getOrderBy());
            Boolean reasonable = page.getReasonable();
            PageHelper.startPage(pageNum, pageSize, orderBy).setReasonable(reasonable);
        }
        return new PageInfo(mapper.orgTypeDetailStatisticRecords(capBill));
    }
}
